package cn.haixiang._02oneway;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.common.message.MessageExt;

import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * @BelongsProject: rocketmq-test
 * @BelongsPackage: cn.itsource._01_hello
 * @Author: Director
 * @CreateTime: 2022-07-23  17:28
 * @Description: 消息消费者
 * @Version: 1.0
 */
public class OnewayConsumer {

    public static void main(String[] args) throws Exception {
        // 1.创建消费者对象，指定消费者组名
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("consumer_oneway_group");
        // 2.连接mq
        consumer.setNamesrvAddr("127.0.0.1:9876");
        // 4.消费者订阅主题，表示此消费者消费哪个主题的消息，二级分类可以是*
        //1.生产者主题。 2.*该主题下面的所有二级，可以指定获取消息
        consumer.subscribe("oneway_topic", "oneway_tag");
        // 5.注册监听器
        consumer.registerMessageListener((List<MessageExt> msgs, ConsumeConcurrentlyContext context) -> {
            for (MessageExt msg : msgs) {
                String result = new String(msg.getBody(), StandardCharsets.UTF_8);
                System.out.println(result);
            }
            // 标记该消息已经被成功消费
            return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
        });
        // 3.启动消费者
        consumer.start();

    }

}